Fix a bug where JSON was lost when saving invalid options

Andrew Cantino 10 years ago
parent
commit
403db15b35

+ 3 - 2
app/assets/javascripts/application.js.coffee.erb

@@ -133,8 +133,9 @@ $(document).ready ->
133 133
 
134 134
         $(".description").html(json.description_html) if json.description_html?
135 135
 
136
-        window.jsonEditor.json = json.options
137
-        window.jsonEditor.rebuild()
136
+        if $("#agent_options").hasClass("showing-default") || $("#agent_options").val().match(/\A\s*(\{\s*\}|)\s*\Z/g)
137
+          window.jsonEditor.json = json.options
138
+          window.jsonEditor.rebuild()
138 139
 
139 140
         $("#agent-spinner").stop(true, true).fadeOut();
140 141
 

+ 2 - 2
app/views/agents/_form.html.erb

@@ -59,8 +59,8 @@
59 59
   <div class="control-group">
60 60
     <%= f.label :options, :class => 'control-label' %>
61 61
     <div class="controls">
62
-      <textarea rows="10" id="agent_options" name="agent[options]" class="span8 live-json-editor">
63
-        <%= ((@agent.new_record? && @agent.options == {}) ? @agent.default_options : @agent.options).to_json %>
62
+      <textarea rows="10" id="agent_options" name="agent[options]" class="span8 live-json-editor <%= (@agent.new_record? && @agent.options == {}) ? "showing-default" : "" %>">
63
+        <%= Utils.jsonify((@agent.new_record? && @agent.options == {}) ? @agent.default_options : @agent.options) %>
64 64
       </textarea>
65 65
     </div>
66 66
   </div>

+ 1 - 1
lib/serialize_and_symbolize.rb

@@ -29,7 +29,7 @@ module SerializeAndSymbolize
29 29
 
30 30
           def #{column_name}=(data)
31 31
             if data.is_a?(String)
32
-              self[:#{column_name}] = JSON.parse(data).recursively_symbolize_keys rescue data
32
+              self[:#{column_name}] = JSON.parse(data).recursively_symbolize_keys rescue {}
33 33
             elsif data.is_a?(Hash)
34 34
               self[:#{column_name}] = data.recursively_symbolize_keys
35 35
             else

+ 2 - 2
vendor/assets/javascripts/jquery.json-editor.js

@@ -501,10 +501,10 @@ JSONEditor.prototype.build = function(json, node, parent, key, root) {
501 501
 
502 502
     for(var i in json){
503 503
       var innerbq = $(document.createElement("BLOCKQUOTE"));
504
-      var newElem = this.editable(i.toString(), i.toString(), json, root, 'key').wrap('<span class="key"></b>').parent();
504
+      var newElem = this.editable(i.toString(), i.toString(), json, root, 'key').wrap('<span class="key"></span>').parent();
505 505
       innerbq.append(newElem);
506 506
       if (newElem) if (newElem.text() == "??") elem = newElem;
507
-      if (typeof json[i] != 'string') {
507
+      if (typeof json[i] != 'string' && typeof json[i] != 'number') {
508 508
         innerbq.prepend(this.braceUI(i, json));
509 509
         innerbq.prepend(this.bracketUI(i, json));
510 510
         if (this._showWipe) innerbq.prepend(this.wipeUI(i, json));